/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import POJO.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Master
 */
public class DAO_GheNgoi {
    DataProvider data;
    
    public List<POJO_GheNgoi> LoadList() throws SQLException {
        data = new DataProvider();
        data.Open();
        
        String query = "SELECT * FROM ghe_ngoi";
        List<POJO_GheNgoi> list = new ArrayList<>();
        ResultSet rs = data.ExecuteQuery(query);
        
        while (rs.next()) {
            POJO_GheNgoi dt = new POJO_GheNgoi();
            // doanTau = new POJO_DoanTau();
            
            dt.setMaGhe(rs.getString("MaGhe"));
            
            //doanTau.setMADT(rs.getString("MaDT"));
            //doanTau.setTENDT(rs.getString("TenDT"));
            dt.setMaDT(rs.getString("MaDT"));
            
            dt.setLoaiGhe(rs.getString("LoaiGhe"));
            dt.setTenToa(rs.getString("TenToa"));
            dt.setMaPhong(rs.getString("MaPhong"));
            dt.setTang(rs.getInt("Tang"));
            
            list.add(dt); 
        }
        
        return list;
    } 
    
    public int xoaGheNgoi(POJO_GheNgoi dt) {
            String sql = String.format("DELETE FROM `ghe_ngoi` WHERE MaGhe = '%s'", dt.getMaGhe());
            data = new DataProvider();
            data.Open();
            
            return data.ExeCuteUpdate(sql);
    }
    
    public int themGheNgoi(POJO_GheNgoi dt) {
            String sql = String.format("INSERT INTO `ghe_ngoi`(`MaGhe`, `MaDT`, `LoaiGhe`, `TenToa`, `MaPhong`, `Tang`) VALUES ('%s','%s','%s','%s','%s','%s')",
                    dt.getMaGhe(),
                    dt.getMaDT(),
                    dt.getLoaiGhe(),
                    dt.getTenToa(),
                    dt.getMaPhong(),
                    dt.getTang());
            data = new DataProvider();
            data.Open();
            
            return data.ExeCuteUpdate(sql);
    } 
    
    public int capNhatGheNgoi(POJO_GheNgoi dt)  {
            String sql = String.format("UPDATE `ghe_ngoi` SET `MaGhe`='%s',`MaDT`='%s',`LoaiGhe`='%s',`TenToa`='%s',`MaPhong`='%s',`Tang`='%s' WHERE MaGhe = '%s'",
                    dt.getMaGhe(),
                    dt.getMaDT(),
                    dt.getLoaiGhe(),
                    dt.getTenToa(),
                    dt.getMaPhong(),
                    dt.getTang(),
                    dt.getMaGhe()
            );
            data = new DataProvider();
            data.Open();
            
            return data.ExeCuteUpdate(sql);
    }
}
